<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class PositionList extends Model
{
    //
    use SoftDeletes;
    
    protected $connection = 'mysql';
	protected $casts = [
        'content' => 'json'
    ];
	
	public static function boot() {
        parent::boot();
        static::updated(function($model) {
			if(isset($model->content) && !isset($model->content['thumb']))
			{
				$content = json_decode($model->original['content']);
				$selfmodel = self::find($model->id);
				$newconent = $model->content;
				foreach($content as $k=>$row)
				{
					if(strstr($k, 'thumb'))
					{
						// dd($k);
						
						$newconent = array_add($newconent, $k, $content->$k);
						
						
					}
				}
				// \Log::info($newconent);
				$selfmodel->content = $newconent;
				$selfmodel->save();
				
			}
			
			
			
			
            
        });
    }
	
	//手动默认字段
	public function getTitleAttribute()
    {
        return $this->content['title'];
    }
	public function getUrlAttribute()
    {
        return $this->content['url'];
    }
    
	public function getThumbAttribute()
    {
        return (isset($this->content['thumb']) && !empty($this->content['thumb']))?thumbOssTitlepicUrl($this->content['thumb']):'';
    }
	public function getDescAttribute()
    {
        return $this->content['desc'];
    }
	//手动默认字段结束
	
	public function fields()
	{
		return $this->hasMany(PositionField::class, 'position_id', 'id');
	}
	
	public function self_lists()
	{
		return $this->hasMany(PositionList::class, 'position_id', 'position_id');
	}
}
